package features.advance.ch3.webserver.demo1;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author LIN
 * @date 2022-09-18 21:44
 */
public class Server {
    public static void main(String[] args) throws IOException
    {
        System.out.println("------------------------------------");
        System.out.println("Server start......");
        System.out.println("------------------------------------");

        ServerSocket server = new ServerSocket(8888);

        while (true)
        {
            Socket client = server.accept();
            OutputStream out = client.getOutputStream();
            InputStream in = client.getInputStream();

            ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
            byte[] temp = new byte[1024];
            int realLen = 0;
            while ((realLen = in.read(temp)) != -1)
            {
                byteArrayOut.write(temp, 0, realLen);
            }

            byte[] recv = byteArrayOut.toByteArray();

            // 将接收的消息,发回给客户端
            out.write(recv);
            out.flush();
        }
    }


}
